package com.hopu.petshop.controller.old;//package com.hopu.petshop.controller;
//
//import com.hopu.petshop.entity.User;
//import com.hopu.petshop.service.UserService;
//import com.hopu.petshop.util.CookieUtil;
//
//import javax.servlet.ServletException;
//import javax.servlet.annotation.WebServlet;
//import javax.servlet.http.HttpServlet;
//import javax.servlet.http.HttpServletRequest;
//import javax.servlet.http.HttpServletResponse;
//import java.io.IOException;
//import java.sql.SQLException;
//
///**
// * 登录Servlet
// */
//@WebServlet("/login.do")
//public class LoginServlet extends HttpServlet {
//
//    //用spring创建service对象
//    private UserService userService = SpringConfig.getContext().getBean(UserService.class);
//
//    @Override
//    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        //设置字符编码
////        req.setCharacterEncoding("UTF-8");
////        resp.setCharacterEncoding("UTF-8");
//        //获得参数
//        String username = req.getParameter("username");
//        String password = req.getParameter("password");
//        String autoLogin = req.getParameter("auto_login");
//        String code = req.getParameter("code");
//        //把用户输入的验证码和session中验证码进行比较
//        String code1 = (String) req.getSession().getAttribute("code");
//        if(code1 == null || !code1.equals(code)){
//            req.setAttribute("error","验证码错误");
//            req.getRequestDispatcher("login.jsp").forward(req,resp);
//            return;
//        }
//        try {
//            User user = userService.login(username, password);
//            if(user == null){
//                //将错误信息保存到request对象
//                req.setAttribute("error","账号或密码错误");
//                //如果登录失败，请求转发到登录页面
//                req.getRequestDispatcher("login.jsp").forward(req,resp);
//            }else{
//                //判断是否免登录
//                if("on".equals(autoLogin)){
//                    int time = 7 * 24 * 3600;
//                    //保存账号密码到Cookie中
//                    CookieUtil.save(resp,"username",username,time);
//                    CookieUtil.save(resp,"password",password,time);
//                }
//                //将用户对象保存到session对象
//                req.getSession().setAttribute("user",user);
//                //如果登录成功，重定向到查询宠物的servlet
//                resp.sendRedirect("findPetsPage.do");
//            }
//        } catch (SQLException e) {
//            e.printStackTrace();
//        }
//    }
//}
